#!/bin/sh
#set -x
cd $(dirname $0)

dir_path=`pwd`
git_ver='no_git_config'

if [ -f .git/config ]; then
    git_ver=`git log -1|head -1|awk -F ' ' '{print $2}'`
fi

cur_date=`date +%Y%m%d`

version_file="${dir_path}/version.h"
v1=1
v2=0
v3=0
v4=1
appkey=100xx_1

name=`pwd`
name=`basename "${name}"`

if [ -f ${version_file} ]; then
    o_v1=`grep -a 'MAJOR_VERSION' ${version_file} |awk 'NR{print $3}'`
    o_v2=`grep -a 'MINOR_VERSION' ${version_file} |awk 'NR{print $3}'`
    o_v3=`grep -a 'REVISE_VERSION' ${version_file} |awk 'NR{print $3}'`
    o_v4=`grep -a 'BUILD_VERSION' ${version_file} |awk 'NR{print $3}'`
    o_git_ver=`grep -a 'GIT_VERSION' ${version_file} |awk 'NR{print $3}'`
    o_cur_date=`grep -a 'BUILD_DATE' ${version_file} |awk 'NR{print $3}'`

    o_git_ver=`echo $o_git_ver | sed 's/"//g'`
    if [ "$o_v1" = "$v1" -a "$o_v2" = "$v2" -a "$o_v3" = "$v3" -a "$o_v4" = "$v4" -a "$o_git_ver" = "$git_ver" ] ; then
        echo "${name}_Build_${v1}.${v2}.${v3}.${v4}_${git_ver}_${o_cur_date}"
        echo "$appkey"
        exit
    fi
fi

app_version_info="${name}_Build_${v1}.${v2}.${v3}.${v4}_${git_ver}_${cur_date}"
echo $app_version_info
echo $appkey

echo "#ifndef __VERSION_H__">$version_file
echo "#define __VERSION_H__">>$version_file
echo "">>$version_file

echo "#define MAJOR_VERSION ${v1}">>$version_file
echo "#define MINOR_VERSION ${v2}">>$version_file
echo "#define REVISE_VERSION ${v3}">>$version_file
echo "#define BUILD_VERSION ${v4}">>$version_file
echo "#define APP_VERSION \"$v1.$v2.$v3.$v4\"">>$version_file
echo "">>$version_file

echo "#define GIT_VERSION \"${git_ver}\"">>$version_file
echo "#define BUILD_DATE ${cur_date}">>$version_file
echo "#define APP_VER_INFO \"${app_version_info}\"">>$version_file
echo "#define APP_KEY \"${appkey}\"">>$version_file
echo "">>$version_file

echo "#endif">>$version_file;
